import { get, post, put, del } from "./api";

class InterviewInvitationService {
  /**
   * 获取所有面试邀请
   * @param {Object} params - 分页参数
   * @returns {Promise<Object>} - 返回面试邀请列表和分页信息
   */
  async getInterviewInvitations(page = 1, pageSize = 10) {
    try {
      const response = await get("/interview-invitations", {
        page,
        pageSize,
      });
      return response;
    } catch (error) {
      console.error("Error fetching interview invitations:", error);
      throw error;
    }
  }

  /**
   * 获取单个面试邀请详情
   * @param {number} id - 面试邀请ID
   * @returns {Promise<Object>} - 返回面试邀请详情
   */
  async getInterviewInvitation(id) {
    try {
      const response = await get(`/interview-invitations/${id}`);
      return response;
    } catch (error) {
      console.error(`Error fetching interview invitation ${id}:`, error);
      throw error;
    }
  }

  /**
   * 创建新的面试邀请
   * @param {Object} data - 面试邀请数据
   * @returns {Promise<Object>} - 返回创建的面试邀请
   */
  async createInterviewInvitation(data) {
    try {
      console.log(
        "Creating interview invitation with data:",
        JSON.stringify(data, null, 2)
      );
      const response = await post("/interview-invitations", data, true);
      console.log("Interview invitation created successfully:", response);
      return response.data || response;
    } catch (error) {
      console.error("Error creating interview invitation:", error);
      if (error.response) {
        console.error("Error response data:", error.response.data);
        console.error("Error response status:", error.response.status);
        console.error("Error response headers:", error.response.headers);
        throw new Error(
          `创建面试邀请失败: ${
            error.response.data?.message ||
            error.response.data?.error ||
            error.response.data ||
            error.message ||
            "服务器内部错误"
          }`
        );
      }
      throw new Error(`创建面试邀请失败: ${error.message || "网络请求失败"}`);
    }
  }

  /**
   * 更新面试邀请
   * @param {number} id - 面试邀请ID
   * @param {Object} data - 更新的数据
   * @returns {Promise<Object>} - 返回更新后的面试邀请
   */
  async updateInterviewInvitation(id, data) {
    try {
      return await put(`/interview-invitations/${id}`, data, true);
    } catch (error) {
      console.error("Error updating interview invitation:", error);
      throw new Error("更新面试邀请失败");
    }
  }

  /**
   * 删除面试邀请
   * @param {number} id - 面试邀请ID
   * @returns {Promise<void>}
   */
  async deleteInterviewInvitation(id) {
    try {
      await del(`/interview-invitations/${id}`);
    } catch (error) {
      console.error("Error deleting interview invitation:", error);
      throw new Error("删除面试邀请失败");
    }
  }

  /**
   * 根据求职者ID获取其所有面试邀请
   * @param {number} jobSeekerId - 求职者ID
   * @returns {Promise<Array>} - 返回面试邀请列表
   */
  async getInvitationsByJobSeeker(jobSeekerId) {
    try {
      return await get(`/interview-invitations/job-seeker/${jobSeekerId}`);
    } catch (error) {
      console.error("Error fetching job seeker invitations:", error);
      throw new Error("获取求职者面试邀请列表失败");
    }
  }

  /**
   * 根据企业职位ID获取所有面试邀请
   * @param {number} companyJobId - 企业职位ID
   * @returns {Promise<Array>} - 返回面试邀请列表
   */
  async getInvitationsByCompanyJob(companyJobId) {
    try {
      return await get(`/interview-invitations/company-job/${companyJobId}`);
    } catch (error) {
      console.error("Error fetching company job invitations:", error);
      throw new Error("获取职位面试邀请列表失败");
    }
  }

  async searchInterviewInvitations(
    searchType,
    searchValue,
    page = 1,
    pageSize = 10
  ) {
    try {
      const response = await get("/interview-invitations/search", {
        searchType,
        searchValue,
        page,
        pageSize,
      });
      return response;
    } catch (error) {
      console.error("Error searching interview invitations:", error);
      throw error;
    }
  }

  /**
   * 发送面试邀请
   * @param {Object} data - 面试邀请数据
   * @returns {Promise<Object>} - 返回创建的面试邀请
   */
  async sendInterviewInvitation(data) {
    try {
      console.log("Sending invitation with data:", data);
      const response = await this.createInterviewInvitation(data);
      console.log("Interview invitation sent successfully:", response);
      return response;
    } catch (error) {
      console.error("Error sending interview invitation:", error);
      throw new Error(
        error.response?.data?.message || "发送面试邀请失败，请重试"
      );
    }
  }
}

export const interviewInvitationService = new InterviewInvitationService();
